<template>
  <ul class="nodes-bar">
    <li>
      <div
        class="start-node"
        data-type="start-node"
        @mousedown="startDrag"
      ></div>
      <p>开始</p>
    </li>

    <li>
      <div
        class="general-node"
        data-type="general-node"
        @mousedown="startDrag"
      ></div>
      <p>审批</p>
    </li>
    <li>
      <div
        class="special-node"
        data-type="special-node"
        @mousedown="startDrag"
      ></div>
      <p>提交</p>
    </li>
    <li>
      <div class="end-node" data-type="end-node" @mousedown="startDrag"></div>
      <p>结束</p>
    </li>
    <!-- <li>
      <div class="shunt-node" data-type="shunt-node" @mousedown="startDrag">
        <img src="../assets/shunt.png" alt />
      </div>
      <p>分流</p>
    </li> -->
    <li style="margin-left: auto">
      <el-button @click="dialogVisible = true" class="btn_save">保存</el-button>
    </li>
    <el-dialog :visible.sync="dialogVisible" width="400px" title="流程信息">
      <el-form
        :model="form"
        ref="processInfo"
        label-width="110px"
        label-position="left"
      >
        <el-form-item
          prop="name"
          label="流程名称"
          :rules="[
            { required: true, message: '流程名称不能为空', trigger: 'blur' },
          ]"
        >
          <el-input v-model="form.name"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button
          type="primary"
          size="mini"
          class="tableAdd"
          @click="exportData"
          >确定</el-button
        >
        <el-button
          class="dialogCancel"
          type="primary"
          size="mini"
          @click="dialogVisible = false"
          >关闭</el-button
        >
      </div>
    </el-dialog>
  </ul>
</template>
<script>
import { Addon } from "@antv/x6";
import { addProcess } from "@/api/approvalManage/processManage.js";
const { Dnd } = Addon;
export default {
  name: "NodesBar",
  props: {
    approvalForm: {
      type: Object,
      default: null,
    },
    submitForm: {
      type: Object,
      default: null,
    },
  },
  data() {
    return {
      dnd: {},
      graph: null,
      dialogVisible: false,
      form: {
        name: "",
      },
      relatesInfo: [],
    };
  },
  methods: {
    startDrag(e) {
      const target = e.currentTarget;
      const type = target.getAttribute("data-type");
      let node;
      if (type === "general-node") {
        node = this.graph.createNode({
          shape: "general-node",
        });
      }
      if (type === "start-node") {
        node = this.graph.createNode({
          shape: "start-node",
        });
      }
      if (type === "special-node") {
        node = this.graph.createNode({
          shape: "special-node",
        });
      }
      node = this.graph.createNode({ shape: type });
      this.dnd.value.start(node, e);
    },
    initDnd(data) {
      this.graph = data;
      this.dnd.value = new Dnd({
        target: data,
        validateNode() {
          return true;
        },
      });
    },
    exportData() {
      this.relatesInfo.push(this.approvalForm);
      this.relatesInfo.push(this.submitForm);
      console.log(this.relatesInfo);
      this.$refs.processInfo.validate((valid) => {
        if (valid) {
          console.log(
            JSON.stringify(this.graph.toJSON({ diff: true }), null, 2)
          );

          // const params = {
          //   description: '',
          //   id: this.$route.query.data,
          //   json_xml: JSON.stringify(this.graph.toJSON({ diff: true }), null, 2),
          //   name: this.form.name,
          //   svg_xml: ''
          // };
          // addProcess(params).then(res => {
          //   this.$message.success('操作成功！')
          //   this.dialogVisible = false;
          // }).catch(err => {
          //   this.$message.error('操作失败！')
          // })
        } else {
          return false;
        }
      });
    },
  },
};
</script>
<style scoped lang="scss">
.nodes-bar {
  width: 100%;
  height: 70px;
  background: #fff;
  border-bottom: 1px solid #efefef;
  display: flex;
  li {
    height: 50px;
    text-align: center;
    color: #666;
    font-size: 14px;
    list-style: none;
    display: flex;
    align-items: center;
    margin-right: 30px;
    p {
      margin-left: 5px;
    }
    .general-node {
      width: 80px;
      height: 40px;
      line-height: 36px;
      text-align: center;
      border: 1px solid #ff7c0e;
      border-radius: 4px;
      margin: 10px auto;
      background: url(../assets/label.png) no-repeat center / 30px;
    }

    .start-node {
      width: 40px;
      height: 40px;
      line-height: 38px;
      border-radius: 50%;
      text-align: center;
      border: 1px solid #06cc76;
      margin: 10px auto;
      background: url("../assets/start.png") no-repeat center / 20px 20px;
    }
    .end-node {
      width: 40px;
      height: 40px;
      line-height: 38px;
      border-radius: 50%;
      text-align: center;
      border: 1px solid #4440aa;
      margin: 10px auto;
      background: url("../assets/end.png") no-repeat center / 20px 20px;
    }
    .special-node {
      width: 80px;
      height: 40px;
      line-height: 36px;
      text-align: center;
      margin: 10px auto;
      border: 1px solid #f33b3d;
      border-radius: 4px;
      background: url("../assets/edit.png") no-repeat center / 20px 20px;
    }
    .shunt-node {
      width: 40px;
      height: 40px;
      line-height: 45px;
      text-align: center;
      transform: rotateZ(45deg);
      margin: 10px auto;
      border: 1px solid #3e8bf8;
      border-radius: 4px;
      img {
        transform: rotateZ(-45deg);
        width: 20px;
        height: 20px;
      }
      // background: url('../assets/edit.png') no-repeat center / 20px 20px;
    }
  }
  .btn_save {
    width: 80px;
    height: 32px;
    background: #971111;
    border-color: #971111;
    color: #fff;
    padding: 0;
    line-height: 32px;
  }
}
</style>
